package com.award.model;

import java.util.Date;

import net.sf.json.JSONObject;

import com.award.jdbc.dao.JdbcDao;
import com.award.tools.SendPost;

public class Commodity {
	private int id;
	private String commodityName;
	private float price;
	private int selectPeopleNum;
	// private static final int totalNum;
	private int total;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	private Date submitTime;
	private String imgURL;
	private int award;
	private int awardUserID;
	private Date openTime;

	public Date getOpenTime() {
		return openTime;
	}

	public void setOpenTime(Date openTime) {
		this.openTime = openTime;
	}

	public Commodity() {
		this.award = 0;
		this.selectPeopleNum = 0;
	}

	public int getAward() {
		return award;
	}

	public void setAward(int award) {
		this.award = award;
	}

	public int getAwardUserID() {
		return awardUserID;
	}

	public void setAwardUserID(int awardUserID) {
		this.awardUserID = awardUserID;
	}

	public String getImgURL() {
		return imgURL;
	}

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getSelectPeopleNum() {
		return selectPeopleNum;
	}

	public void setSelectPeopleNum(int selectPeopleNum) {
		this.selectPeopleNum = selectPeopleNum;
	}

	public Date getSubmitTime() {
		return submitTime;
	}

	public void setSubmitTime(Date submitTime) {
		this.submitTime = submitTime;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCommodityName() {
		return commodityName;
	}

	public void setCommodityName(String commodityName) {
		this.commodityName = commodityName;
	}

	public String jSONObject() {
		JSONObject json = new JSONObject();
		json.put("jsonrpc", "2.0");
		json.put("method", "generateSignedIntegers");
		JSONObject params = new JSONObject();
		params.put("apiKey", "ac77da41-0142-40a0-b083-d4cdaf93149a");
		params.put("n", 1);
		params.put("min", 1);
		params.put("max", this.total);
		params.put("replacement", true);
		params.put("base", 10);
		json.put("params", params);
		json.put("id", 27272);
		return json.toString();
	}

	public int judge() {
		if (this.selectPeopleNum >= this.total) {
			return selectAward();
		} else
			return -1;
	}

	private int selectAward() {
		Relationship re = null;
		String url = "https://api.random.org/json-rpc/1/invoke";
		JSONObject json = new JSONObject(SendPost.sendPost(url, jSONObject()));
		int raffleNum = (int) json.getJSONObject("result")
				.getJSONObject("random").getJSONArray("data").get(0);

		JdbcDao jd = new JdbcDao();
		try {
			re = jd.queryRelationshipByLuckyNum(raffleNum, this.id);
		} catch (Exception e) { // TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(raffleNum);
		return re.getUserID();
	}
	/*
	 * public static int judge(int nowNum, int commodityID) {//
	 * �����н���id������������-1 if (nowNum >= totalNum) { // ���н��˷�����Ϣ
	 * return selectAward(commodityID); } else return -1; }
	 * 
	 * private static int selectAward(int commodityID) {// ��ȡ�н��� Relationship
	 * re = null; String url = "https://api.random.org/json-rpc/1/invoke";
	 * JSONObject json = new JSONObject(SendPost.sendPost(url, jSONObject()));
	 * int raffleNum = (int) json.getJSONObject("result")
	 * .getJSONObject("random").getJSONArray("data").get(0); JdbcDao jd = new
	 * JdbcDao(); // ����ݿ��ж�ȡ�齱����Ϣ����re_list��ͨ��commodityID����UserID try
	 * { re = jd.queryRelationshipByLuckyNum(raffleNum, commodityID); } catch
	 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
	 * 
	 * return re.getUserID(); }
	 */
}
